package m1;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {

    public static void main(String[] args) throws Exception {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);   // 默认端口可以不设置
        f.setUsername("admin");
        f.setPassword("admin");
        Channel channel = f.newConnection().createChannel();

        // 定义队列
        channel.queueDeclare("helloworld", false, false, false, null);

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("消费者1收到:" + msg);
                System.out.println("----------处理完毕----------");
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };

        // 接收消息
        channel.basicConsume("helloworld", true,
                deliverCallback,   //处理消息的回调对象
                cancelCallback);   // 取消消息处理的回调对象

    }
}
